(function (aopc) {
    aopc.addExpandPage(802, function (get) {
        const Common = get('UtilsCommon', 'Common');
        const Bridge = get('UtilsCommon', 'Bridge');

        return {
            expandInit() {
                this.subjects = this.getProperty('Subjects');
                this.container_view = this.getProperty('container_view');
                this.selectUserDialog1 = this.getProperty('selectUserDialog1');
                this.uploadFile1 = this.getProperty('uploadFile1');
                this.loading_view = this.getProperty('loading_view');
                this.isAsr = this.getProperty('IsAsr');
            },
            setInit() {
                this.isAsr.isVisible = this.visitUser.IsAsr === 1;
                this.setOnOffProperty(this.isAsr);
            },
            setChecked({ property }) {
                property.setValue(!property.value);
            },
            setOnOffProperty(p) {
                const theme = this.theme;

                p.isEdit = true;
                p.setValue = function (v) {
                    this.value = v;
                    const p2 = this.properties[0];
                    if (theme === 'red') p2.style.backgroundColor = v ? '#F92A2A' : '#EECACA';
                    else p2.style.backgroundColor = v ? '#2A9DF4' : '#BAC8DC';
                    p2.style.justifyContent = v ? 'flex-end' : 'flex-start';
                    p2.refresh && p2.refresh();
                };
                p.getValue = function () { return this.value ? 1 : 0; };

            },
            expandDataLoad({ data }) {
                this.isUpdate = data && data.MeetingId;
                if (!this.isUpdate) {
                    this.subjects.setValue(this.getInitSubjects());
                }
                else {
                    this.isAsr.setValue(data.IsAsr);
                }
            },
            showUploadFile({ property }) {
                this.uploadFile1.pageAxis.show(property);
            },
            setReporter({ property }) {
                this.setDialogProperty(property, '选择汇报人', true, property.data.ReporterList);
            },
            setAttendUsers({ property }) {
                this.setDialogProperty(property, '选择列席人员', false, property.data.UserList);
            },
            setDialogProperty(property, title, isReporter, dataSource) {
                if (!property.obj) property.obj = {
                    setSelectUsers: () => {
                        this.setSelectUsers(property);
                    },
                    title,
                    isMultiple: true,
                    isReporter,
                    dataSource
                };
                this.selectUserDialog1.pageAxis.showUsersDialog(property.obj);
            },
            setSelectUsers(property) {
                const list = property.obj.dataSource;
                if (property.obj.isReporter) {
                    const p = property.properties[2];

                    const names = [];
                    const ids = [];
                    list.forEach(d => {
                        names.push((d.JobName || '') + d.Name);
                        ids.push(d.UserId);
                    })
                    property.data.Reporters = ids.join(',');

                    p.setValue(names);
                }
                else {
                    const p = property.properties[1];
                    p.setValue(`列席人员 ${list.length}`);
                    property.data.UserIds = list.map(m => m.UserId).join(',');
                }
            },
            getInitSubjects() {
                return [{
                    ReporterName: '',
                    UserCountName: '列席人员 0',
                    delete_subject_isVisible: false,
                    SubjectId: Common.createGuid()
                }]
            },
            setSubjectProperties({ view, index }) {
                const p = this.getViewProperty(view, 'No');
                if (p) {
                    const i = index + 1;
                    p.text = i < 10 ? '0' + i : i;
                    if (p.setValue) setTimeout(() => p.setValue(p.text), 0);
                }
                const files = this.getViewProperty(view, 'Files');
                files.setDataLoad = this.setFilesDataLoad.bind(this);
            },
            setFilesDataLoad(dataList, property) {
                const view = property.getView();
                const fileCount2 = this.getViewProperty(view, 'FileCount2');
                fileCount2.data.FileCount2 = dataList.length;
                this.setRefreshValue(fileCount2, dataList.length);
            },
            setFileRow({ data, view }) {
                const file_icon = this.getViewProperty(view, 'file_icon');
                if (file_icon) {
                    file_icon.imgUrl = this.getFileIcon(data);
                }
            },
            getFileIcon(data) {
                const t = data.FileType.toLowerCase();
                const vt = '.mp4,.mov,.avi,.mkv,.rmvb';
                const it = '.jpg,.jpeg,.png,.gif,.svg';
                if (t === '.doc' || t === '.docx') return "/images/yxinmt_h5/25080313563175553.png";
                else if (t === '.xls' || t === '.xlsx') return "/images/yxinmt_h5/25080313565026910.png";
                else if (t === '.ppt' || t === '.pptx') return "/images/yxinmt_h5/25080313564674186.png";
                else if (t === '.pdf') return "/images/yxinmt_h5/25080313563543435.png";
                else if (vt.indexOf(t) >= 0) return '/images/yxinmt_h5/25080521052203829.png';
                else if (it.indexOf(t) >= 0) return '/images/yxinmt_h5/25080521052598726.png';
                else return "/images/yxinmt_h5/25080521052936371.png";
            },
            setRefreshValue(p, v) {
                p.value = v;
                p.refresh && p.refresh();
            },
            getDefaultRowData() {
                return {
                    ReporterName: '',
                    UserCountName: '列席人员 0',
                    SubjectId: Common.createGuid()
                }
            },
            uploadCallback({ res, property }) {
                const files = property.getView().getView().properties[2];
                res.FileId = Common.createGuid();
                files.add(res);
            },
            setFiles(dataList, property) {
                const files = property.getView().getView().properties[2];
                files.addList(dataList);
            },
            setImportTemplateFiles(dataList, property) {
                const entityData = this.container_view.entityData || {};
                entityData.Title = property.meetingTitle;
                const subjects = [];
                const obj = {};
                dataList.forEach(d => {
                    const subjectName = d.DirNames.split('/')[1];
                    obj[subjectName] = obj[subjectName] || [];
                    obj[subjectName].push(d);
                });
                for (let key in obj) {
                    subjects.push({
                        Name: key,
                        UserCountName: '列席人员 0',
                        ReporterName: '',
                        Files: obj[key],
                        SubjectId: Common.createGuid()
                    });
                }
                entityData.Subjects = (entityData.Subjects || []).concat(subjects);
                this.eventActions.entityEdit.setPropertiesValue(this.container_view.properties, entityData, false, true);
            },
            expandSetEntityData({ entityData }) {
                entityData.MeetingId = this.pageData.MeetingId || Common.createGuid();
                entityData.MeetingRoomId = this.visitUser.MeetingRoomId;
                if (window.loginUser && !this.isUpdate) entityData.AdminUserId = window.loginUser.UserId;
                if (this.visitUser.IsAsr) {
                    entityData.IsAsr = this.isAsr.getValue();
                }
                const subjects = entityData.Subjects;
                const names = {};
                for (let i = 0; i < subjects.length; i++) {
                    const s = subjects[i];
                    if (Common.isNullOrEmpty(s.Name)) {
                        this.alert('请输入会议议题！')
                        return false;
                    }
                    if (!names[s.Name]) names[s.Name] = 0;
                    names[s.Name]++;
                    if (names[s.Name] > 1) {
                        this.alert('会议议题不能重复！')
                        return false;
                    }

                    const fileNames = {}
                    for (let j = 0; j < s.Files.length; j++) {
                        const f = s.Files[j];

                        if (!fileNames[f.FileName]) fileNames[f.FileName] = 0;
                        fileNames[f.FileName]++;
                        if (fileNames[f.FileName] > 1) {
                            this.alert('文件' + f.FileName + '重复上传！')
                            return false;
                        }
                    }
                }
                this.entityData = entityData;
                return entityData;
            },
            successCallback({ data }) {
                const meetingId = data.MeetingId || this.pageData.MeetingId;
                this.alertSuccess('保存成功！', () => {
                    const url = '/meetingSubjectList.html?isStart=true&MeetingId=' + meetingId;

                    const callback = () => {
                        this.alertSuccess('所有终端开始会议', () => {
                            this.toPage(url);
                            this.webSocket.send('toPage', { url });
                        }, 1500);
                    };
                    if (this.entityData.IsAsr) this.startMeetingAsr(meetingId, callback);
                    else callback();
                });
            },
            successCallback2({ data }) {
                const meetingId = data.MeetingId || this.pageData.MeetingId;
                this.alertSuccess('保存成功！', () => {
                    this.toPage('/meetingRoomLayout.html?MeetingId=' + meetingId);
                });
            },
            importTemplate({ property }) {
                Bridge.importTemplate((data) => {
                    property.meetingTitle = data.Title;
                    this.uploadFile1.pageAxis.show(property, data);
                });
            },
            downloadTemplate() {
                Bridge.downloadTemplate();
            },
            setFileExpand({ property, view }) {
                const view2 = view.getView().getView().getView();
                const files = view2.properties[2];
                files.isVisible = !files.isVisible;
                files.setIsVisible(files.isVisible);
                property.setValue(files.isVisible ? '文件收起' : '文件展开');
            }
        }
    });
})(window.aopc);